/**
 * Filename: NativeStickyGridAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/10 0:37
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.NativePreviewActivity;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.fragment.NativeAlbumFragment;
import com.github.postsanf.yinian.nativestick.StickyGridHeadersSimpleAdapter;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.view.NativeImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.List;

public class NativeStickyGridAdapter extends BaseAdapter implements
        StickyGridHeadersSimpleAdapter{

    private boolean isSelectStatus;     // 是否在选择状态
    private List<YNNativeImage> hasHeaderIdList;
    private LayoutInflater mInflater;
    private ImageLoader imageLoader;
    private Context     mContext;
    private NativeAlbumFragment fragment;
    private Resources   mResource;

    public void setResource(Resources mResource){
        this.mResource = mResource;
    }

    public NativeStickyGridAdapter(Context mContext, List<YNNativeImage> hasHeaderIdList,boolean isSelectStatus,NativeAlbumFragment fragment){
        mInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.hasHeaderIdList = hasHeaderIdList;
        this.isSelectStatus = isSelectStatus;
        this.fragment = fragment;
        imageLoader = ImageLoader.getInstance();
        if (YNSingleton.getInstance().getNativePhoto().size()==0 || YNSingleton.getInstance().getNativePhoto()!=hasHeaderIdList){
            YNSingleton.getInstance().setNativePhoto(hasHeaderIdList);
        }
    }

    public void setSelectStatus(boolean isSelectStatus){
        this.isSelectStatus = isSelectStatus;
    }

    @Override
    public int getCount() {
        return hasHeaderIdList.size();
    }

    @Override
    public Object getItem(int position) {
        return hasHeaderIdList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.yn_item_native_image, parent, false);
            mViewHolder.mImageView = (SimpleDraweeView) convertView.findViewById(R.id.native_item_image);
            mViewHolder.mNative_item_select = (ImageButton) convertView.findViewById(R.id.native_item_select);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        final String path = hasHeaderIdList.get(position).getPath();
        mViewHolder.mImageView.setTag(path);

        mViewHolder.mImageView.setImageResource(R.drawable.pic_null);

        if (mResource!=null){
            mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("picture_unselected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
        }else{
            mViewHolder.mNative_item_select.setImageResource(R.drawable.picture_unselected);
        }

        Uri uri = Uri.parse("file://" + path);
        ImageOptHelper.showFrescoNative(uri, mViewHolder.mImageView, mContext);

        // 设置图片宽高
        int screenWidth = DisplayUtils.getScreenWidthPixels((Activity) mContext);
        int horizontalSpacing = DisplayUtils.dp2px(mContext, 2);
        int itemWidth = (screenWidth - 2 * horizontalSpacing) / 4;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
        mViewHolder.mImageView.setLayoutParams(params);

        if(isSelectStatus){
            mViewHolder.mNative_item_select.setVisibility(View.VISIBLE);
            mViewHolder.mImageView.setColorFilter(null);
            // 图片选择事件
            mViewHolder.mNative_item_select.setOnClickListener(new View.OnClickListener() {
                //选择，则将图片变暗，反之则反之
                @Override
                public void onClick(View v) {
                    // 已经选择过该图片
                    if (SelectedImg.mSelectedImage.contains(path)){
                        SelectedImg.mSelectedImage.remove(path);
                        if (mResource!=null){
                            mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("picture_unselected", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
                        }else{
                            mViewHolder.mNative_item_select.setImageResource(R.drawable.picture_unselected);
                        }
                        mViewHolder.mImageView.setColorFilter(null);
                    } else
                    // 未选择该图片
                    {
                        SelectedImg.mSelectedImage.add(path);
                        if (mResource!=null){
                            mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
                        }else{
                            mViewHolder.mNative_item_select.setImageResource(R.drawable.pictures_selected);
                        }
                        mViewHolder.mImageView.setColorFilter(Color.parseColor("#33000000"));
                    }
                    fragment.setSureCount(SelectedImg.mSelectedImage.size());
                }
            });
            // 响应选择效果
            if (SelectedImg.mSelectedImage.contains(path))
            {
                if (mResource!=null){
                    mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
                }else{
                    mViewHolder.mNative_item_select.setImageResource(R.drawable.pictures_selected);
                }
                mViewHolder.mImageView.setColorFilter(Color.parseColor("#33000000"));
            }
        }else{
            mViewHolder.mNative_item_select.setVisibility(View.GONE);
            mViewHolder.mImageView.setColorFilter(null);
        }
        // 点击图片大图查看
        mViewHolder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NativePreviewActivity.class);
                intent.putExtra(CommonConstants.YNDATAS, position);
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder mHeaderHolder;

        if (convertView == null) {
            mHeaderHolder = new HeaderViewHolder();
            convertView = mInflater.inflate(R.layout.yn_item_native_image_header, parent, false);
            mHeaderHolder.mTextView = (TextView) convertView
                    .findViewById(R.id.native_header_time);
            convertView.setTag(mHeaderHolder);
        } else {
            mHeaderHolder = (HeaderViewHolder) convertView.getTag();
        }
        mHeaderHolder.mTextView.setText(hasHeaderIdList.get(position).getTime());

        return convertView;
    }

    //  获取HeaderId,HeaderId不同则New一个
    @Override
    public long getHeaderId(int position) {
        return hasHeaderIdList.get(position).getSection();
    }

    public static class ViewHolder {
        public SimpleDraweeView mImageView;
        public ImageButton     mNative_item_select;
    }

    public static class HeaderViewHolder {
        public TextView mTextView;
    }
}